package CA;
import java.io.File;
import java.util.ArrayList;
import java.util.List;



public class SequenceCheck {

	private final String CrashSuperLocation =System.getProperty("user.dir") + "/UnitTestData/SequenceCheck2/";
	
	public static void main(String[] args) {
		SequenceCheck st = new SequenceCheck();
		st.Run();
	}
	
	
	private void Run()
	{	
		Helper h = new Helper();
		
		List<IndividualFileContents> indFileList = createMasterSequenceList(CrashSuperLocation);
		List<SSM_MissingData> missingList = createSSM_MissingData(indFileList);	
		
		listMatching(missingList, indFileList);
		
		
		
		/*
		 // This is just for checking to see if the list was built correctly.
		for (int i = 0; i < indFileList.size(); i++) {
			System.out.println("\n"+indFileList.get(i).getFilePath());
			for (int ii = 0; ii < indFileList.get(i).getValues().length; ii++) {
			// (folder)[line]
				System.out.println(indFileList.get(i).getValues()[ii]);
			}
		}
		*/
		
		// Build Missing Crash info
	}
	
	
	
	// This should be made into a cleaner function
	public void listMatching(List<SSM_MissingData> missingSSMDataList, List<IndividualFileContents> indFileList)
	{
	//	System.out.println("Missing:" + missingSSMDataList.size());
	//	System.out.println("Total:" + indFileList.size());
	
	
		
		// loop through each of the missing items, see which ones match up
		
		
		
	//	for (int i=0; i<missingSSMDataList.size();i++){
			//if (isMatchErrorOneLine(missingSSMDataList.get(i).getPreviousInfo(),missingSSMDataList.get(i).getNextLineInfo())==true){
				
			//}
			System.out.println(getMatchErrorOneLine(missingSSMDataList.get(0).getPreviousInfo(),missingSSMDataList.get(0).getNextLineInfo(), indFileList));
	//	}
	}
	
	// Look at refactoring this in the future. 
	private String getMatchErrorOneLine(String prevLine, String nextLine, List<IndividualFileContents> indFileList){
	
//		System.out.println(prevLine);
//		System.out.println(nextLine);
		
	//	System.out.println("hi dan");
		
//		System.out.println(indFileList.size());
		
		for (int i=0; i<indFileList.size();i++){
			for (int ii = 0; ii < indFileList.get(i).getValues().length; ii++) {
				// print out every line of every file
			//	System.out.println(indFileList.get(i).getValues()[ii]);
				
				if (indFileList.get(i).getValues()[ii] == prevLine){
				//	System.out.println("potential match" + indFileList.get(i).getValues()[ii]);
					// make sure that there are at least two more lines in the file
					
					System.out.println("match:" + ii +"Dan:" + indFileList.get(i).getValues().length);
					
					// make sure that the bottom matches too
						if (1==1){
					//		System.out.println("fill in:" + indFileList.get(i).getValues()[ii+1]);
						}
					
					
				}
				
				
				
			}
		//	System.out.println("");
		}
	
		
		
		
	//	System.out.println(indFileList.get(0).getValues()[1]);
		
		return "";
	}
	
	
	// Get match middle
	
	
	
	
	public List<SSM_MissingData> createSSM_MissingData(List<IndividualFileContents> indFileList)
	{
		Helper h = new Helper();
		List<SSM_MissingData> missingSSMDataList = new ArrayList<SSM_MissingData>();
		
		// Now cycle through the master list to find missing info
		for (int i = 0; i < indFileList.size(); i++) {
		//	System.out.println("\n"+indFileList.get(i).getFilePath());
			for (int ii = 0; ii < indFileList.get(i).getValues().length; ii++) {
			// (folder)[line]
			//	System.out.println(indFileList.get(i).getValues()[ii]);
				
				if (h.isMissingModule(indFileList.get(i).getValues()[ii])==true){
				
					String tempPreviousInfo="";
					String tempNextLineInfo="";
				
					if (ii >1){
						tempPreviousInfo = indFileList.get(i).getValues()[ii-1];
					}
						
					if (ii+1 < indFileList.get(i).getValues().length){
						tempNextLineInfo= indFileList.get(i).getValues()[ii+1];
					}
					missingSSMDataList.add(new SSM_MissingData(indFileList.get(i).getFilePath(),tempPreviousInfo,tempNextLineInfo,indFileList.get(i).getValues()[ii]));		
				}
			}
		}
		
		return missingSSMDataList;
	}
	
	// Put all of the contents of the file into a list
	public List<IndividualFileContents> createMasterSequenceList(String CrashSuperLocation)
	{
		Helper h = new Helper();
		List<IndividualFileContents> indFileList = new ArrayList<IndividualFileContents>();
		List<String> AllFiles=new ArrayList<String>();	
		AllFiles = h.getAllFileNames(CrashSuperLocation);
		
		for (int i = 0; i < AllFiles.size(); i++) {
			//	System.out.println(h.getCompleteFileContents(new File(AllFiles.get(i))));
			indFileList.add(new IndividualFileContents(AllFiles.get(i),h.getCompleteFileContentsWithLineBreak(new File(AllFiles.get(i)))));		
		}
		/*
		System.out.println(indFileList.get(0).getValues()[3]);
		System.out.println(indFileList.get(1).getValues()[3]);
		System.out.println(indFileList.get(2).getValues()[3]);
		System.out.println(indFileList.get(3).getValues()[3]);
		System.out.println(indFileList.get(4).getValues()[3]);
		*/
		return indFileList;
	}
	
	
}
